# -*- coding: UTF-8 -*-

from .env import Environment

from .controllers.web import WEB
from .ui.action import Addons


class RPC(object):
    def __init__(self, base_url, timeout, dbname, addons_list=None):
        self.env = Environment(self)
        self.web = WEB(base_url, timeout)

        self.addons = Addons(addons_list)

        self.base_url = base_url
        self.timeout = timeout
        self.dbname = dbname
        self.addons_models = {}

    # def version_info(self):
    #     print('version_info')
    #     return self.web.webclient.version_info()

    # def version(self):
    #     print('version')
    #     return self.web.webclient.version()

    def login(self, username, password):
        db = self.dbname
        res = self.web.session.authenticate(
            db=self.dbname, login=username, password=password)

        return res

    def call(self, model, method, *args):
        return self.call_kw(model, method, args, {})

    def call_kw(self, model, method, args, kwargs):
        context = kwargs.get('context', {})
        context = context.copy()
        ctx = self.env.context
        context.update(ctx)
        kwargs['context'] = context
        res = self.web.dataset.call_kw(model, method, args, kwargs)
        return res
